//
//  MyCFHostTestViewController.m
//  MyCFHostTest
//
//  Created by wilson_chen on 2011/1/27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyCFHostTestViewController.h"
#import <CFNetwork/CFNetwork.h>

@implementation MyCFHostTestViewController
@synthesize btnGetIp;
@synthesize txtUrl;
@synthesize txtIp;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
}



/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	btnGetIp = nil;
	txtUrl = nil;
	txtIp = nil;
}


- (void)dealloc {
	[btnGetIp release];
	[txtUrl release];
	[txtIp release];
    [super dealloc];
}

- (IBAction)btnGetIpAction:(UIButton*)sender {
	//const char *cStr;
	//cStr = [txtUrl.text cStringUsingEncoding:[NSString defaultCStringEncoding]];
	CFStringRef strUrlRef = CFSTR("dvr_8187c7.ddns.iview-ddns.com");
	CFHostRef HostRef = CFHostCreateWithName(NULL, strUrlRef);
	CFStreamError *errors;
	Boolean resolved = CFHostStartInfoResolution(HostRef, kCFHostNames, errors);
	if (resolved) {
		NSLog(@"StartInfoResolution : YES");
	}
	else {
		NSLog(@"StartInfoResolution : NO");
	}
	CFArrayRef IpArrayRef = CFHostGetAddressing(HostRef, &resolved);
	if (resolved) {
		NSLog(@"GetIPAddress : YES");
	}
	else {
		NSLog(@"GetIPAddress : NO");
	}
	CFDataRef IpDataRef = CFArrayGetValueAtIndex(IpArrayRef, 0);
	UInt8 *strBytes;
	CFDataGetBytes(IpDataRef, CFRangeMake(0, CFDataGetLength(IpDataRef)), strBytes);
	NSString *strIP = [[NSString alloc] initWithBytes:strBytes length:CFDataGetLength(IpDataRef) encoding:[NSString defaultCStringEncoding]];
	txtIp.text = strIP;
}

@end
